package com.librariy.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.text.TextUtils;

import java.util.List;

public class MyLocation {
    protected static final String TAG = MyLocation.class.getSimpleName();
    private static LocationManager mLocationManager;
    private static MyLocationListener mMyLocationListener=new MyLocationListener();
    
    public static Location getLastKnownLocation(Context mContext) {
        if (mLocationManager == null&&mContext!=null) {
            mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
            MyLocation.rebind();
        }
        return mMyLocationListener.lastLocation;
    }
    @SuppressLint("MissingPermission")
    private static boolean rebind(){
        if (mLocationManager == null){
            Log.e(TAG, String.format("rebind(mLocationManager=%s)", mLocationManager + ""));
            return false;
        }
        List<String> providerList=mLocationManager.getAllProviders();
        mLocationManager.removeUpdates(mMyLocationListener);
        for(String provider:providerList){
            if (TextUtils.isEmpty(provider)||LocationManager.PASSIVE_PROVIDER.equals(provider)) continue;
            mLocationManager.requestLocationUpdates(provider, 5*1000, 1, mMyLocationListener);
            mMyLocationListener.lastLocation = mLocationManager.getLastKnownLocation(provider);
            Log.d(TAG, String.format("rebind(mLocationManager=%s, provider=%s)", mLocationManager + "", provider+""));
        }
        return true;
    }
    // 监听器
    private static class MyLocationListener implements LocationListener{        
        private Location lastLocation = new Location(LocationManager.GPS_PROVIDER);
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            String sStatus="";
            if(status==LocationProvider.AVAILABLE){
                sStatus="有效";
            }else if(status==LocationProvider.OUT_OF_SERVICE){
                sStatus="不再服务区";
            }else if(status==LocationProvider.TEMPORARILY_UNAVAILABLE){
                sStatus="暂停服务";
            }
            Log.d(TAG, String.format("onStatusChanged(provider=%s,  Status=%s)", provider + "", sStatus + ""));
        }

        @Override
        public void onProviderEnabled(String provider) {
            Log.d(TAG, String.format("onProviderEnabled(%s)", provider + ""));
        }

        @Override
        public void onProviderDisabled(String provider) {
            Log.d(TAG, String.format("onProviderDisabled(%s)", provider + ""));
        }

        @Override
        public void onLocationChanged(Location location) {
            this.lastLocation = location;
            if(location==null){
                Log.d(TAG, "onLocationChanged(null)");
                return;
            }
            Log.d(TAG, String.format("onLocationChanged(经度:%s,  经度:%s,  时间:%s)", location.getLongitude() + "", location.getLatitude() + "", location.getTime() + ""));
        }
    }
}
